package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.cache.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.service.*;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/strategies")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyThemeService strategyThemeService;

    @Autowired
    private IStrategyCommentService strategyCommentService;
    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;

    @GetMapping("/detail")
    public Object detail(String id){

        //攻略阅读数+1
        strategyStatisVORedisService.viewnumIncrease(id,1);

        return JsonResult.success(strategyService.get(id));
    }
    @GetMapping("/query")
    public Object query(StrategyQuery qo){
        return JsonResult.success(strategyService.query(qo));
    }
    //查询主题
    @GetMapping("/themes")
    public Object themes(){
        return JsonResult.success(strategyThemeService.list());
    }
    //评论添加
    @RequireLogin
    @PostMapping("/addComment")
    public Object addComment(StrategyComment comment ,@UserParam UserInfo userInfo){
        //设置用户信息
        //comment.setCity(userInfo.getCity());

        //属性拷贝
        //参数1 源对象   参数2 目标对象
        //将源对象属性拷贝到目标对象中去   属性名词必须相同
        //原理  内省机制
        BeanUtils.copyProperties(userInfo,comment);
        //用户id无法拷贝  属性不一样
        comment.setUserId(userInfo.getId());
        strategyCommentService.save(comment);

        //评论数+1
        strategyStatisVORedisService.replynumIncrease(comment.getStrategyId(),1);

        return JsonResult.success();
    }
    //评论分页
    @GetMapping("/comments")
    public Object comments(StrategyCommentQuery qo){
        return JsonResult.success(strategyCommentService.query(qo));
    }
    //评论点赞
    @RequireLogin
    @PostMapping("/commentThumb")
    public Object commentThumb(String cid,@UserParam UserInfo userInfo){
        strategyCommentService.commentThumb(cid,userInfo.getId());
        return JsonResult.success();
    }
    @GetMapping("/statisVo")
    public Object statisVo(String sid){
        return JsonResult.success(strategyStatisVORedisService.getStrategyStatisVo(sid));
    }
    //攻略收藏
    @RequireLogin
    @PostMapping("/favor")
    public Object favor(String sid,@UserParam UserInfo userInfo){
        boolean ret = strategyStatisVORedisService.favor(sid,userInfo.getId());
        //如果为true 表示为收藏操作  false表示取消收藏操作
        return JsonResult.success(ret);
    }
    //攻略点赞
    @RequireLogin
    @PostMapping("/strategyThumbup")
    public Object strategyThumbup(String sid,@UserParam UserInfo userInfo){
        boolean ret = strategyStatisVORedisService.strategyThumbup(sid,userInfo.getId());
        //如果为true 表示为顶成功操作  false表示已顶操作
        return JsonResult.success(ret);
    }
}
